<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="./js/local.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" v-model="keyword" @keyup.enter="handleEnter">
        <h3>历史搜索<button @click="handleDeleteAll">全部删除</button></h3>
        <p v-for="item of historys">
            {{item}}
            <button @click="handleDelete(item)">删除</button>
        </p>
    </div>
    <script>
        new Vue({
            data() {
                return {
                    keyword: "",
                    historys: []
                }
            },
            methods: {
                handleEnter() {
                    if (!this.historys.includes(this.keyword)) {
                        this.historys.push(this.keyword)
                        setLocalStorage('historys', this.historys)
                        this.keyword = '';
                    }else{
                        // 如果搜索的值已经存在数组里 则放置于数组顶部
                        
                    }
                    console.log(this.historys);
                },
                handleDelete(val) {
                    var index = this.historys.indexOf(val);
                    this.historys.splice(index, 1)
                    setLocalStorage('historys', this.historys)
                },
                handleDeleteAll(){
                    this.historys = '';
                    setLocalStorage('historys', this.historys)
                }
            },
            mounted() {
                var historys = getLocalStorage('historys');
                if (historys) {
                    this.historys = historys
                }
            },
        }).$mount("#app")
    </script>
</body>

</html>